const version = `v1.0.1`
const version_text = `增加静态壁纸，修复已知错误。`
if (localStorage.getItem("livebg") == "true") {
    var bg = document.createElement("video");
    bg.src = localStorage.getItem("livebg-src") || "https://cdn.jsdelivr.net/gh/MobiusBeta/assets/videos/Live_Wallpaper_2.mp4";
    bg.id = "livebg";
    bg.autoplay = "true";
    bg.loop = "true";
    bg.muted = "true";
    getID("bkg").appendChild(bg);
    getID("livebg").style.opacity = 0;
    getID("livebg").style.display = "block";
    getID("livebg").onload = function () {
        getID("livebg").style.opacity = 1;
    }

} else {
    var bg = document.createElement("img");
    bg.src = localStorage.getItem("bkg-src") || "https://retiehe.com/backend/bing/1080p";
    bg.id = "bg";
    bg.style.filter = "brightness(.8)";
    getID("bkg").appendChild(bg);
    getID("bg").style.opacity = 0;
    getID("bg").style.display = "block";
    getID("bg").onload = function () {
        getID("bg").style.opacity = 1;
    }

}

function aboutProject() {
    swal({
        title: `关于`,
        text: `这是 New Page 的全新版本 (V3)，它具有全新的界面以及更多的功能。\n\nVersion: ${version};\nCopyright: 2021 YFun (@oCoke);\nRelease Note: ${version_text}`,
        button: "Close",
    });
}


if (!localStorage || !window.localStorage) {
    swal({ text: "您的浏览器不支持 localStorage，可能无法正常使用。", icon: "error" })
}
function getID(label) {
    return document.getElementById(label);
}



/**
 * FOCUS
 */
checkFocus();
function checkFocus() {
    var FocusLabel = ['searchInput', 'searchEngine', 'google', 'baidu', 'bing', 'custom']
    if (FocusLabel.includes(document.activeElement.id)) {
        getID("searchEngine").style.opacity = 1;
        getID("app").style["backdrop-filter"] = "blur(10px)";
        getID("app").style["-webkit-backdrop-filter"] = "blur(10px)";
        getID("poet").style.opacity = 1;
    } else {
        getID("searchEngine").style.opacity = 0;
        getID("app").style["backdrop-filter"] = "none";
        getID("app").style["-webkit-backdrop-filter"] = "none";
        getID("poet").style.opacity = 0;
    }
}

/* 获取时间 */
function getNowDate() {
    Date.prototype.toLocaleString = function () {
        var M = this.getMinutes();
        M = M > 9 ? M : "0" + M;
        return this.getHours() + ":" + M;
    };
    return new Date().toLocaleString();
}
getID("nowDateTime").innerHTML = getNowDate();
document.title = "开始 - " + getNowDate();
window.setInterval(function () {
    getID("nowDateTime").innerHTML = getNowDate();
    document.title = "开始 - " + getNowDate();
}, 1000)

/* 搜索 */
function google() {
    console.log("Toggle to Google.");
    getID("searchForm").action = "https://www.google.com/search";
    getID("searchInput").name = "q";
    getID("google").style.backgroundColor = "rgba(255,255,255,.3)";
    getID("baidu").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("bing").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("custom").style.backgroundColor = "rgba(255,255,255,.1)";
}

function baidu() {
    console.log("Toggle to Baidu.");
    getID("searchForm").action = "https://baidu.com/s";
    getID("searchInput").name = "wd";
    getID("baidu").style.backgroundColor = "rgba(255,255,255,.3)";
    getID("google").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("bing").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("custom").style.backgroundColor = "rgba(255,255,255,.1)";
}

function bing() {
    console.log("Toggle to Bing.");
    getID("searchForm").action = "https://bing.com/search";
    getID("searchInput").name = "q";
    getID("bing").style.backgroundColor = "rgba(255,255,255,.3)";
    getID("google").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("baidu").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("custom").style.backgroundColor = "rgba(255,255,255,.1)";
}

function custom() {
    console.log("Toggle to Custom.");
    getID("searchForm").action = localStorage.getItem("customSearchURL");
    getID("searchInput").name = localStorage.getItem("customSearchKEY");
    getID("custom").style.backgroundColor = "rgba(255,255,255,.3)";
    getID("google").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("bing").style.backgroundColor = "rgba(255,255,255,.1)";
    getID("baidu").style.backgroundColor = "rgba(255,255,255,.1)";
}

if (localStorage.getItem("customSearchURL") && localStorage.getItem("customSearchKEY")) {
    getID("custom").innerHTML = localStorage.getItem("customSearchNAME") || "&#xe631;";
    getID("custom").style = null;
}
if (localStorage.getItem("defaultSearch")) {
    eval(localStorage.getItem("defaultSearch") + " ();");
} else {
    baidu();
}



const fetchPoet = (url) => {
    if (!window.fetch) {
        console.warn("浏览器不支持 Fetch API。")
    } else {
        fetch(url, { method: 'GET' }).then((resp) => {
            return Promise.all([resp.ok, resp.status, resp.text(), resp.headers]);
        }).then(([ok, status, data, headers]) => {
            if (ok) {
                return {
                    ok,
                    status,
                    data,
                    headers
                }
            } else {
                throw new Error(JSON.stringify(json.error));
            }
        }).then((resp) => {
            let data = JSON.parse(resp.data);
            // console.log(data);
            getID("poetText").innerText = `「${data.hitokoto}」`;
            getID("poetAuthor").innerText = `——${data.from_who}《${data.from}》`;
        }).catch((error) => {
            console.warn(error);
        });
    }
};

fetchPoet("https://v1.hitokoto.cn/?c=i&encode=json&charset=utf-8")

